package com.chenyun.common.utils;

import cn.hutool.core.util.ZipUtil;

import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileUtil extends cn.hutool.core.io.FileUtil {


    /**
     * 根据文件名获取文件名称和后缀
     * @param fileName
     * @return
     */
    public static String [] getFileNameAndSuffix(String fileName){
        String suffix = getSuffix(fileName);
        String replace = fileName.replace("." + suffix, "");
        return new String[]{replace,suffix};
    }




    /**
     * 打包文件
     * @param zipFIlePath
     * @param srcFiles
     * @return
     */
    public static  File  zip(String zipFIlePath, File... srcFiles)
    {
        File zip = ZipUtil.zip(file(zipFIlePath), false, srcFiles);
        return zip;
    }

    /**
     * 生成zip文件,根据文件路径不带子文件夹(全局可用)
     * @param zipfullPath 压缩后的zip文件全路径
     * @param fileFullPaths 压缩前的文件全路径数组
     */
    public static void createZip(String zipfullPath,String[] fileFullPaths) {
        InputStream inputStream = null;
        ZipOutputStream zip = null;
        try {
            zip = new ZipOutputStream(new FileOutputStream(zipfullPath));
            for(String fullPath:fileFullPaths) {

                if(StringUtils.isBlank(fullPath)) {
                    continue;
                }

                //创建文件
                File file = new File(fullPath);
                String fileName = file.getName();

                //读文件流
                inputStream = new BufferedInputStream(new FileInputStream(file));
                byte[] buffer = new byte[inputStream.available()];
                inputStream.read(buffer);
                inputStream.close();

                //将读取的文件输出到zip中
                zip.putNextEntry(new ZipEntry(fileName));
                zip.write(buffer);
                zip.closeEntry();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                if (zip != null) {
                    zip.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * 生成zip文件,根据文件路径不带子文件夹(全局可用)
     * @param zipfullPath 压缩后的zip文件全路径
     * @param files 压缩前的文件全路径数组
     */
    public static File createZip(String zipfullPath,List<File> files) {
        InputStream inputStream = null;
        ZipOutputStream zip = null;
        File zipFile = new File(zipfullPath) ;	// 定义压缩文件名称
        try {
            zip = new ZipOutputStream(new FileOutputStream(zipFile));
            for(File file:files) {
                String fileName = file.getName();

                //读文件流
                inputStream = new BufferedInputStream(new FileInputStream(file));
                byte[] buffer = new byte[inputStream.available()];
                inputStream.read(buffer);
                inputStream.close();
                //将读取的文件输出到zip中
                zip.putNextEntry(new ZipEntry(fileName));
                zip.write(buffer);
                zip.closeEntry();
            }

            return zipFile;
        } catch (Exception e) {
            e.printStackTrace();

        }
        finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                if (zip != null) {
                    zip.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return  null;
    }









    /**
     * 压缩文件
     *
     * @param sourceFilePath 源文件路径
     * @param zipFilePath    压缩后文件存储路径
     * @param zipFilename    压缩文件名
     */
    public static File compressToZip(String sourceFilePath, String zipFilePath ) {
        File sourceFile = new File(sourceFilePath);
        File zipPath = new File(zipFilePath);
        if (!zipPath.exists()) {
            zipPath.mkdirs();
        }
        File zipFile = new File(zipPath.getPath());
//        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
//            writeZip(sourceFile, "", zos);
//            //文件压缩完成后，删除被压缩文件
//            boolean flag = deleteDir(sourceFile);
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException(e.getMessage(), e.getCause());
//        }
        return zipFile;
    }

    /**
     * 遍历所有文件，压缩
     *
     * @param file       源文件目录
     * @param parentPath 压缩文件目录
     * @param zos        文件流
     */
    public static void writeZip(File file, String parentPath, ZipOutputStream zos) {
        if (file.isDirectory()) {
            //目录
            parentPath += file.getName() + File.separator;
            File[] files = file.listFiles();
            for (File f : files) {
                writeZip(f, parentPath, zos);
            }
        } else {
            //文件
            try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
                //指定zip文件夹
                ZipEntry zipEntry = new ZipEntry(parentPath + file.getName());
                zos.putNextEntry(zipEntry);
                int len;
                byte[] buffer = new byte[1024 * 10];
                while ((len = bis.read(buffer, 0, buffer.length)) != -1) {
                    zos.write(buffer, 0, len);
                    zos.flush();
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage(), e.getCause());
            }
        }
    }

        /**
         * 删除文件夹
         *
         * @param dir
         * @return
         */
        public static boolean deleteDir(File dir) {
            if (dir.isDirectory()) {
                String[] children = dir.list();
                for (int i = 0; i < children.length; i++) {
                    boolean success = deleteDir(new File(dir, children[i]));
                    if (!success) {
                        return false;
                    }
                }
            }
            //删除空文件夹
            return dir.delete();
        }





    }
